package com.zprmb.udzt;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.support.v4.hardware.fingerprint.FingerprintManagerCompat;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.zprmb.udzt.http.AjaxParams;
import com.zprmb.udzt.util.BitmapUtils;
import com.zprmb.udzt.util.Constant;
import com.zprmb.udzt.util.CustomDialog_view;
import com.zprmb.udzt.util.FingerprintUtil;
import com.zprmb.udzt.util.VolleryUtil;

import org.json.JSONObject;

import java.net.URLEncoder;

public class V6_Finger_CheckActivity extends GeActivity {
private ImageView img_finger_bg;
private TelephonyManager tm;
private SharedPreferences sha;
private CustomDialog_view.Builder builder;
private View view_finger;
private TextView text_login;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.v6_finger_check);
		BbtApplication.getInstance().addActvity(this);
		sha=getSharedPreferences("config", MODE_PRIVATE);
		tm = (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE);
		img_finger_bg=(ImageView)findViewById(R.id.img_finger_bg);
		img_finger_bg.setImageBitmap(BitmapUtils.readBitMap(this, R.drawable.bg_finger));
		view_finger=(View)findViewById(R.id.view_finger);
		text_login=(TextView)findViewById(R.id.text_login);
		text_login.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(V6_Finger_CheckActivity.this,V6LoginActivity.class);
				startActivity(intent);
				finish();
			}
		});
		view_finger.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				onFingerprintClick(null);
			}
		});
		if(isFinger()){
			onFingerprintClick(null);
		}
	}
	public void onFingerprintClick(View v){

        FingerprintUtil.callFingerPrint(new FingerprintUtil.OnCallBackListenr() {
           
            @Override
            public void onSupportFailed() {
               showToast("当前设备不支持指纹");
            }

            @Override
            public void onInsecurity() {
                showToast("当前设备未处于安全保护中");
            }

            @Override
            public void onEnrollFailed() {
                showToast("请到设置中设置指纹");
            }

            @Override
            public void onAuthenticationStart() {
                builder = new CustomDialog_view.Builder(V6_Finger_CheckActivity.this);
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        FingerprintUtil.cancel();
                        builder.create().dismiss();
                    }
                });
                builder.setMessage("“优点智投”的 Touch ID");
                builder.setMessage2("请验证已有手机指纹");
                builder.create().setCancelable(false);
               builder.create().show();
               Log.e("dialog", builder.create()+"");
            }

            @Override
            public void onAuthenticationError(int errMsgId, CharSequence errString) {
                showToast(errString.toString());
//                Log.e("dialog", builder.create()+"");
                if (builder.create() != null){
                	builder.create().mdissmis();
                }
            }

            @Override
            public void onAuthenticationFailed() {
                showToast("解锁失败");
            }

            @Override
            public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
                showToast(helpString.toString());
            }

            @Override
            public void onAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResult result) {
                showToast("解锁成功");
                Log.e("dialog", builder.create()+"");
//                Log.e("object", builder.create()+""+builder.create().isShowing()+"");
                if (builder.create() != null  ){
                	builder.create().mdissmis();
                    upData("FINGERPRINT");
                }

            }
        });
    }
	private void upData(String type) {
		if(!network()){
			finish();
		}
		AjaxParams params = new AjaxParams();
		params.put("loginName", sha.getString("mobile", ""));
		params.put("loginType", type);
		params.put("appVersion", getAppVersion());
		try {
			params.put("cipher", URLEncoder.encode(sha.getString("password", ""), "UTF-8"));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		try {
//			ToastUtil.showCenterToast(V6_Finger_CheckActivity.this,  URLEncoder.encode(sha.getString("password", ""), "UTF-8"));
//		} catch (UnsupportedEncodingException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		VolleryUtil vollery = new VolleryUtil() {
			
			@Override
			public void onUpDate(JSONObject jsonObject) {
				// TODO Auto-generated method stub
				Log.e("ssssg", jsonObject.toString());
					JSONObject json1=jsonObject.optJSONObject("data");
					if (jsonObject.optInt("success") == 1) {
						
						Editor editor = sha.edit();
						editor.putString(
								"token",
								json1.optString(
										"token"));
						editor.putString(
								"userId",
								json1.optString(
										"userId"));
						editor.putString(
								"photoUrl",
								json1.optString(
										"photoUrl"));
						editor.putString(
								"mobile",
								json1.optString(
										"mobile"));
						editor.putString(
								"usercode",
								json1.optString(
										"userCode"));
						editor.putString("loginName", getIntent().getStringExtra("phone"));
						editor.putInt("islogin", 1);
						editor.commit();






						V6_Finger_CheckActivity.this.finish();
						
						
						
						
						
					} else {




						if (jsonObject.optString("comment").equals("please login")) {
							Intent intent = new Intent(V6_Finger_CheckActivity.this,V6LoginActivity.class);
							startActivity(intent);
						}
						V6_Finger_CheckActivity.this.finish();
						
					}
				
			}

			@Override
			public void onError() {
				// TODO Auto-generated method stub
				V6_Finger_CheckActivity.this.finish();
			}
		};
		vollery.setHeaders("appVersion", getAppVersion());
		vollery.setHeaders("token", sha.getString("token", ""));
		vollery.setHeaders("client", "android");
		vollery.setHeaders("deviceToken",tm.getDeviceId());
		vollery.setHeaders("mobileType", getDeviceType());
		vollery.setRequestMethod(1);
		vollery.setShow(true);
		vollery.execute(this, Constant.URL + "/api/v6/login/app?",
				params.toString());
	}




}
